package com.cloudRestudy.hystrix.controller;

import com.cloudRestudy.common.core.result.R;
import com.cloudRestudy.hystrix.service.HystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName : HystrixController
 * @Author : lby
 * @Date: 2021/7/21 18:01
 * @Description : 断路器
 */
@RestController
@AllArgsConstructor
@RequestMapping("/hystrix")
public class HystrixController {
    private final HystrixService hystrixService;

    @GetMapping("/success-msg")
    public R successMsg(){
        return  hystrixService.successMsg();
    }


    /**
     * 测试服务降级 hystrix
     *  @HystrixCommand(fallbackMethod = "") 当服务出现异常，用于fallback的方法
     * @return
     */
    @GetMapping("/error-msg")
    public R errorMsg(){
        return hystrixService.errorMsg();
    }


    /**
     * 熔断器测试
     * 出错时可以自动回复
     * 错误率过高时，出现正确的也无法回复，需要回复时间
     * @param randomId
     * @return
     */
    @GetMapping("/cir-test/{randomId}")
    public R cirBreakerTest(@PathVariable("randomId") Integer randomId) throws Exception {
        return  hystrixService.circuitBreakerTest(randomId);
    }
}
